<?php
// $Id$

/**
 * @file
 * The guts of the theme :)
 */

require_once('theme-settings.php');

/**
 * Add Custom Generated CSS File
 * This file is generated each time the theme settings page is loaded.
 */
$custom_css = file_directory_path() .'/sky/custom.css';
if (file_exists($custom_css)) {
  drupal_add_css($custom_css, 'theme', 'all', TRUE);
}

/**
 * Implementation of hook_theme().
 * This function provides a one-stop reference for all
 */
function sky_theme(&$existing, $type, $theme, $path) {
  return array(
    'breadcrumb' => array(
      'arguments' => array('breadcrumb' => array()),
      'file' => 'functions/theme-overrides.inc',
    ),
    'conditional_stylesheets' => array(
      'file' => 'functions/theme-custom.inc',
    ),
    'feed_icon' => array(
      'arguments' => array('url' => NULL, 'title' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'form_element' => array(
      'arguments' => array('element' => NULL, 'value' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'fieldset' => array(
      'arguments' => array('element' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'menu_local_tasks' => array(
      'arguments' => NULL,
      'file' => 'functions/theme-overrides.inc',
    ),
    'menu_item_link' => array(
      'arguments' => array('link' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'more_link' => array(
      'arguments' => array('url' => array(), 'title' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'pager' => array(
      'arguments' => array('tags' => array(), 'limit' => NULL, 'element' => NULL, 'parameters' => array(), 'quantity' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
   'status_messages' => array(
      'arguments' => array('display' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'status_report' => array(
      'arguments' => array('requirements' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'table' => array(
      'arguments' => array('header' => NULL, 'rows' => NULL, 'attributes' => array(), 'caption' => NULL),
      'file' => 'functions/theme-overrides.inc',
    ),
    'render_attributes' => array(
      'arguments' => array('attributes'),
      'file' => 'functions/theme-custom.inc',
    ),
  );
}

/**
 * Implementation of hook_preprocess().
 *
 * @param $vars
 * @param $hook
 * @return Array
 */
function sky_preprocess(&$vars, $hook) {

  // Only add the admin.css file to administrative pages
  if (arg(0) == 'admin') {
    drupal_add_css(path_to_theme() .'/css/admin.css', 'theme', 'all', TRUE);
  }

 /**
  * This function checks to see if a hook has a preprocess file associated with
  * it, and if so, loads it.
  */
  if (is_file(drupal_get_path('theme', 'sky') .'/preprocess/preprocess-'. str_replace('_', '-', $hook) .'.inc')) {
    include('preprocess/preprocess-'. str_replace('_', '-', $hook) .'.inc');
  }
}

function sky_poll_choices($form) {
  global $user;

  if ($form) {
    drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-poll-more").val("'. t('Add another choice') .'"); }); }', 'inline');

    $rows = array();
    /*$headers = array(
      t('Choice'),
      t('Vote count'),
    );*/
    $headers[0] = t('Choice');
    if (user_access('administer nodes')) {
      $headers[1] = t('Vote count');
    }

    foreach (element_children($form) as $key) {
      // No need to print the field title every time.
      unset($form[$key]['chtext']['#title'], $form[$key]['chvotes']['#title']);

      // Build the table row.
      /*$row = array(
        'data' => array(
          array('data' => drupal_render($form[$key]['chtext']), 'class' => 'poll-chtext'),
          //array('data' => drupal_render($form[$key]['chvotes']), 'class' => 'poll-chvotes'),
        ),
      );*/
      $row['data'][0] = array('data' => drupal_render($form[$key]['chtext']), 'class' => 'poll-chtext');
      if (user_access('administer nodes')) {
        $row['data'][1] = array('data' => drupal_render($form[$key]['chvotes']), 'class' => 'poll-chvotes');
      }

      // Add additional attributes to the row, such as a class for this row.
      if (isset($form[$key]['#attributes'])) {
        $row = array_merge($row, $form[$key]['#attributes']);
      }
      $rows[] = $row;
    }

    $output = theme('table', $headers, $rows);
    $output .= drupal_render($form);
    return $output;
  }
  //return _phptemplate_callback('breadcrumb', $variables);
}